package action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;

import classes.Rayon;

import sessionfactory.HibernateSessionFactory;

public class ListRayonsAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception 
			{
				Session session = HibernateSessionFactory.getSessionFactory().openSession();
				
				String login = request.getUserPrincipal().getName();
				String req = new String ("select e.idEmploye From Employe As e where e.login='");
				req += login + "'";
				List<Integer> idEmploye = session.createQuery(req).list();
				request.setAttribute("idemp", idEmploye.get(0).toString());
				
				req = "select g.idRayon From Gere as g where g.idEmploye='" + 
				idEmploye.get(0).toString() + "'" ;
				List<Integer> rayons = session.createQuery(req).list();
				request.setAttribute("idrayon", rayons.get(0).toString());
				
				req = "From Rayon as r where r.idRayon='" + 
				rayons.get(0).toString() + "'" ;
				List<Rayon> liste = session.createQuery(req).list();
				request.setAttribute("listeRayons", liste);
				
		        return mapping.findForward("success");
			}
}
